Logical Operators
if <condition> and <condition>:
<if body>
if <condition> or <condition>:
<if body>
if not(<condition>):
<if body>
Allows for multiple conditions to be combined at the same time.
- Operators:
- and :
- If all conditions are satisfied, the if body is executed.
- or :
- If at least one of the conditions is satisfied, the if body is executed.
- not :
- If the reverse of the condition is satisfied, the if body is executed.
- Returns:
- True or False.
- Return Type:
- Boolean
Index | ID | Species | Color | Weight | Age |
---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 |
1 | cat_001 | cat | golden | 1.5 | 0.2 |
2 | cat_002 | cat | black | 15 | 9 |
3 | dog_002 | dog | white | 80 | 2 |
4 | dog_003 | dog | black | 25 | 0.5 |
5 | ham_001 | hamster | black | 1 | 3 |
6 | ham_002 | hamster | golden | 0.25 | 0.2 |
7 | cat_003 | cat | black | 10 | 0 |
def more_descriptive_name(id_str, species, color, weight, age):
return id_str + ': This ' + color + ' ' + species + ' weighs ' + weight + ' lbs and is ' + age + ' years old'
def cat_and_dog_info(pet_id):
id_arr = np.array(pets.get('ID'))
if pet_id not in id_arr:
return 'This pet is not in our record'
pets_info = pets[pets.get('ID') == pet_id]
age = pets_info.get('Age').iloc[0]
weight = pets_info.get('Weight').iloc[0]
species = pets_info.get('Species').iloc[0]
color = pets_info.get('Color').iloc[0]
if (species == 'dog') and (age < 1.5):
return pet_id + ': This is a puppy 🐶'
elif (species == 'cat') and (age < 1):
return pet_id + ': This is a kitten 🐱'
elif (species == 'dog') or (species == 'cat'):
weight = str(weight)
age = str(age)
return more_descriptive_name(pet_id, species, color, weight, age)
cat_and_dog_info('dog_001')
'dog_001: This black dog weighs 40.0 lbs and is 5.0 years old'
cat_and_dog_info('cat_001')
'cat_001: This is a kitten 🐱'
cat_and_dog_info('cat_009')
'This pet is not in our record'